namespace Core
{
    using System;
    using System.Globalization;
    using Base;
    using Contracts.Interfaces;

    /// <summary>
    /// Extension condition implementation class
    /// </summary>
    /// <typeparam name="T">The type of of IExtension</typeparam>
    
    public sealed class ExtensionCondition<T> : PropertyChangedBase, ICondition<IExtension>, IEquatable<ExtensionCondition<T>> where T : IExtension, new()
    {
        private readonly Lazy<T> _extension;
        private readonly ushort _level;
        /// <summary>
        /// Create a new instance of the <see cref="T:Core.ExtensionCondition`1"/> class.
        /// </summary>
        private ExtensionCondition(){}
        /// <summary>
        /// Create a new instance of the <see cref="T:Core.ExtensionCondition`1"/> class.
        /// </summary>
        public ExtensionCondition(ushort level)
        {
            if (level == 0 || level > 10) throw new ArgumentOutOfRangeException("level");
            _extension = new Lazy<T>();
            _level = level;
        }

        /// <summary>
        /// Obtains the condition extension
        /// </summary>
        public IExtension Extension
        {
            get { return _extension.Value; }
        }

        /// <summary>
        /// Obtains the extension condition level
        /// </summary>
        public ushort Level
        {
            get { return _level; }
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ExtensionCondition<T> other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other._extension.Value, _extension.Value) && other._level == _level;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj.GetType() == typeof (ExtensionCondition<T>) && Equals((ExtensionCondition<T>) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "{0} ({1})", _extension.Value, _level);
        }
    }
}